package com.ruoyi.produce.process.costApportionment.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.produce.process.costApportionment.domain.*;
import com.ruoyi.produce.process.costApportionment.mapper.CostApportionmentMapper;
import com.ruoyi.utils.ID.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 在产成本费用分摊
 * 业务处理层
 */
@Service
public class CostApportionmentServiceImpl implements ICostApportionmentService {
    @Autowired
    private CostApportionmentMapper costApportionmentMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 在产成本费用分摊查询
     *
     * @param cond
     * @return
     */
    @Override
    public List<CostApportionmentDto> selectCostApportionmentList(CostApportionmentCond cond) {
        List<CostApportionmentDto> costApportionmentDtos = costApportionmentMapper.selectCostApportionmentList(cond);
        for (CostApportionmentDto s : costApportionmentDtos) {
            double laborCost = 0.00; //总人工费
            double auxiliaryMaterialCost = 0.00; //总辅料费用
            double otherCost = 0.00; //总其他费用
            if (StringUtils.isNotNull(costApportionmentMapper.selectCostApportionmentListListById(s.getCostApportionmentId()))) {
                // 存在子表信息
                List<CostApportionmentListDto> costApportionmentLists = costApportionmentMapper.selectCostApportionmentListListById(s.getCostApportionmentId());
                for (CostApportionmentListDto sl : costApportionmentLists) {
                    laborCost += sl.getLaborCost(); //累加子表的人工费
                    auxiliaryMaterialCost += sl.getAuxiliaryMaterialCost(); //累加子表的辅料费用
                    otherCost += sl.getOtherCost(); //累加子表的其他费用
                    sl.setTotalCost(sl.getLaborCost() + sl.getAuxiliaryMaterialCost() + sl.getOtherCost());
                }
                s.setCostApportionmentLists(costApportionmentLists);
            }
            s.setLaborCost(laborCost); //主表的总人工费
            s.setAuxiliaryMaterialCost(auxiliaryMaterialCost); //主表的总辅料费用
            s.setOtherCost(otherCost); //主表的总其他费用
        }
        return costApportionmentDtos;
    }

    /**
     * 按编号查询
     *
     * @param costApportionmentId 在产成本费用分摊编号
     * @return
     */
    @Override
    public CostApportionmentDto selectCostApportionmentById(String costApportionmentId) {
        CostApportionmentDto costApportionmentDto = costApportionmentMapper.selectCostApportionmentById(costApportionmentId);
        double laborCost = 0.00; //总人工费
        double auxiliaryMaterialCost = 0.00; //总辅料费用
        double otherCost = 0.00; //总其他费用
        if (StringUtils.isNotNull(costApportionmentMapper.selectCostApportionmentListListById(costApportionmentDto.getCostApportionmentId()))) {
            // 存在子表信息
            List<CostApportionmentListDto> costApportionmentLists = costApportionmentDto.getCostApportionmentLists();
            for (CostApportionmentListDto sl : costApportionmentLists) {
                laborCost += sl.getLaborCost(); //累加子表的人工费
                auxiliaryMaterialCost += sl.getAuxiliaryMaterialCost(); //累加子表的辅料费用
                otherCost += sl.getOtherCost(); //累加子表的其他费用
                sl.setTotalCost(sl.getLaborCost() + sl.getAuxiliaryMaterialCost() + sl.getOtherCost());
            }
            costApportionmentDto.setCostApportionmentLists(costApportionmentLists);
        }
        costApportionmentDto.setLaborCost(laborCost); //主表的总人工费
        costApportionmentDto.setAuxiliaryMaterialCost(auxiliaryMaterialCost); //主表的总辅料费用
        costApportionmentDto.setOtherCost(otherCost); //主表的总其他费用
        return costApportionmentDto;
    }

    /**
     * 在产成本费用分摊新增
     *
     * @param costApportionment
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCostApportionment(CostApportionmentDomain costApportionment){
        String costApportionmentId = idUtil.createCostApportionmentId();//自动生成编号
        if (StringUtils.isNotNull(costApportionmentMapper.selectCostApportionmentById(costApportionmentId))) {
            //存在
            return 0;
        }
        // 不存在
        costApportionment.setCostApportionmentId(costApportionmentId); //主表添加编号
        costApportionmentMapper.insertCostApportionment(costApportionment);
        List<CostApportionmentListDomain> costApportionmentLists = costApportionment.getCostApportionmentLists(); //费用分摊明细列表
        for (CostApportionmentListDomain s : costApportionmentLists) {
            s.setCostApportionmentId(costApportionmentId); //子表添加编号
            costApportionmentMapper.insertCostApportionmentList(s);
        }
        return 1;
    }

    /**
     * 在产成本费用分摊更新
     *
     * @param costApportionment
     * @return
     */
    @Override
    public int updateCostApportionment(CostApportionmentDomain costApportionment) throws Exception {
        if (!StringUtils.isNotNull(costApportionmentMapper.selectCostApportionmentById(costApportionment.getCostApportionmentId()))) {
            //不存在
            return 0;
        }
        // 存在
        costApportionmentMapper.updateCostApportionment(costApportionment); //更新主表
        List<CostApportionmentListDto> costApportionmentListOld = costApportionmentMapper.selectCostApportionmentListListById(costApportionment.getCostApportionmentId());
        List<CostApportionmentListDomain> costApportionmentListNew = costApportionment.getCostApportionmentLists();
        for (int i = 0; i < costApportionmentListNew.size(); i++) {
            CostApportionmentListDto costApportionmentListDto = costApportionmentMapper.selectCostApportionmentListById(costApportionment.getCostApportionmentId(), costApportionmentListNew.get(i).getPlanSelfId());
            if (StringUtils.isNotNull(costApportionmentListDto)) {
                for (int j = 0; j < costApportionmentListOld.size(); j++) {
                    if (costApportionmentListOld.get(j).getProduceId().equals(costApportionmentListDto.getProduceId())) {
                        costApportionmentListOld.remove(j);
                        break;
                    }
                }
                // 更新
                costApportionmentMapper.updateCostApportionmentList(costApportionmentListNew.get(i));
            } else {
                // 新增
                costApportionmentMapper.insertCostApportionmentList(costApportionmentListNew.get(i));
            }
        }
        // 说明有删除的对象
        for (int i = 0; i < costApportionmentListOld.size(); i++) {
            costApportionmentMapper.deleteCostApportionmentListById(costApportionmentListOld.get(i).getCostApportionmentId(), costApportionmentListOld.get(i).getPlanSelfId());
        }
        return 1;
    }

    /**
     * 更新在产成本费用分摊删除标志位
     *
     * @param costApportionment
     * @return
     */
    @Override
    public int updateCostApportionmentDeleteFlag(CostApportionmentDomain costApportionment) {
        return costApportionmentMapper.updateCostApportionmentDeleteFlag(costApportionment);
    }

    /**
     * 在产成本费用分摊详细查询
     *
     * @param cond
     * @return
     */
    @Override
    public List<CostApportionmentListDto> selectCostApportionmentListList(CostApportionmentCond cond) {
        return costApportionmentMapper.selectCostApportionmentListList(cond);
    }

    /**
     * 查询在产成本费用分摊对应的所有计划详细
     *
     * @param costApportionmentId 在产成本费用分摊编号
     * @return
     */
    @Override
    public List<CostApportionmentListDto> selectCostApportionmentListListById(String costApportionmentId) {
        return costApportionmentMapper.selectCostApportionmentListListById(costApportionmentId);
    }

    /**
     * 查询在产成本费用分摊对应的指定产品详细
     *
     * @param costApportionment
     * @return
     */
    @Override
    public CostApportionmentListDto selectCostApportionmentListById(String costApportionment, String planSelfId) {
        return costApportionmentMapper.selectCostApportionmentListById(costApportionment, planSelfId);
    }

    /**
     * 根据生产计划编号查询是否有费用分摊记录
     * @param planSelfId
     * @return
     */
    @Override
    public boolean selectCostApportionmentByPlanSelfId(String planSelfId) {
        return StringUtils.isNotNull(costApportionmentMapper.selectCostApportionmentListListByPlanSelfId(planSelfId));
    }
}
